import opt from "@/assets/js/option";
import {Toast} from 'vant'

export default {
    showLoading(txt) {
        Toast.loading({
            duration: 0, // 持续展示 toast
            forbidClick: true,
            message: txt,
        });
    },
    hideLoading() {
        Toast.clear();
    },
    toast(txt){
        Toast(txt);
    },
    filterTimeFn(stringTimestamp, format = "YYYY-MM-DD HH:mm:SS") {
        if (!stringTimestamp) return "";
        if ((stringTimestamp + "").length <= 10)
            stringTimestamp = stringTimestamp * 1000;
        let time = new Date(stringTimestamp);

        let Check = (obj) => {
            if (obj < 10) {
                return "0" + obj;
            }
            return obj;
        };
        let resFormat = format;
        let y = Check(time.getFullYear()); // 年
        let m = Check(time.getMonth() + 1); // 月
        let d = Check(time.getDate()); // 日
        let h = Check(time.getHours()); // 时
        let mm = Check(time.getMinutes()); // 分
        let ss = Check(time.getSeconds()); // 分

        // resFormat = resFormat.toUpperCase()
        resFormat = resFormat.replace(/YYYY/, y);
        resFormat = resFormat.replace(/MM/, m);
        resFormat = resFormat.replace(/DD/, d);
        resFormat = resFormat.replace(/HH/, h);
        resFormat = resFormat.replace(/mm/, mm);
        resFormat = resFormat.replace(/SS/, ss);

        return resFormat;
    },
    filterOpt(id, type) {
        const options = opt[type];
        const find = options.find((item) => item.value == id);
        return find && find.text;
    },

    parkTimeFilter(val) {
        const year = parseInt(val / 1440);
        const hour = parseInt((val % 1440) / 60);
        const min = (val % 1440) % 60;
        return `${year}天${hour}小时${min}分`;
    },
};
